package cn.com.fuhuitong.main.access.ui.fragment

import cn.com.fuhuitong.base.BaseAbstractFragment
import com.jakewharton.rxbinding2.widget.RxTextView
import cn.com.fuhuitong.R
import cn.com.fuhuitong.app.AppConstance
import com.blankj.utilcode.util.SPStaticUtils
import io.reactivex.Observable
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable
import io.reactivex.functions.BiFunction
import kotlinx.android.synthetic.main.fragment_access_mobile.*

class AccessMobileFragment : BaseAbstractFragment() {

    private val dispose: CompositeDisposable = CompositeDisposable()

    companion object {
        fun newInstance(): AccessMobileFragment {
            return AccessMobileFragment()
        }
    }

    override val layoutResId: Int
        get() = R.layout.fragment_access_mobile

    override fun initView() {

        val observablePhone: Observable<CharSequence> =
            RxTextView.textChanges(edit_mobile_phone)
                .doOnNext {
                    text_mobile_code.isEnabled = it.isNotEmpty()
                }
        val observableCode: Observable<CharSequence> =
            RxTextView.textChanges(edit_mobile_code)

        val observable: Disposable = Observable.combineLatest(observablePhone, observableCode,
            BiFunction<CharSequence, CharSequence, Boolean> { t1, t2 ->
                t1.isNotEmpty() && t2.isNotEmpty()
            })
            .subscribe {
                text_mobile_login_mobile.isEnabled = it
            }
        dispose.add(observable)


        check_mobile_select.setOnCheckedChangeListener { buttonView, isChecked ->
            SPStaticUtils.put(AppConstance.BUNDLE_USER_AGREE, isChecked)
        }

        check_mobile_select.isChecked =
            SPStaticUtils.getBoolean(AppConstance.BUNDLE_USER_AGREE, (false))
    }

    override fun onDestroyView() {
        super.onDestroyView()
        if (dispose.size() > 0 && !dispose.isDisposed) {
            dispose.dispose()
        }
    }
}